from django.shortcuts import render, redirect, reverse
from django.views.decorators.http import require_POST
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required

from xadmin.models import ArticleGroup, Article
from xadmin.forms import ArticleGroupForm
from xadmin.plugins.auditlog import add_log


@login_required
def articlegroup_list(request):
    siteid = request.GET.get('siteid')
    mbx = ('内容管理', '文章组')
    articlegroup_list = ArticleGroup.objects.filter(site_id=siteid)
    context = {
        'siteid': siteid,
        'articlegroup_list': articlegroup_list,
        'mbx': mbx
    }
    return render(request, 'xadmin/articlegroup/list.html', context)


@login_required
def articlegroup_add(request):
    siteid = request.GET.get('siteid')
    mbx = ('内容管理', '文章组', '新建文章组')
    if request.method == "GET":
        article_group_form = ArticleGroupForm.ArticleGroupForm()

        context = {
            'siteid': siteid,
            'article_group_form': article_group_form,
            'mbx': mbx
        }
        return render(request, 'xadmin/articlegroup/add.html', context)
    else:
        article_group_form = ArticleGroupForm.ArticleGroupForm(request.POST, request.FILES)
        if article_group_form.is_valid():
            new_article_group = article_group_form.save(commit=False)
            new_article_group.site_id = siteid
            new_article_group.save()
            action = '添加了文章组《%s》' % new_article_group.name
            add_log(request, action, siteid)
            return redirect('%s?siteid=%s' % (reverse('articlegrouplist'), siteid))
        else:
            error = article_group_form.errors
            context = {
                'siteid': siteid,
                'article_group_form': article_group_form,
                'error': error,
                'mbx': mbx
            }
            return render(request, 'xadmin/articlegroup/add.html', context)


@login_required
def articlegroup_edit(request, articlegroupid):
    siteid = request.GET.get("siteid")
    mbx = ('内容管理', '文章组', '编辑文章组')
    article_group_obj = ArticleGroup.objects.get(id=articlegroupid)
    if request.method == "GET":
        article_group_form = ArticleGroupForm.ArticleGroupForm(instance=article_group_obj)
        context = {
            'siteid': siteid,
            'article_group_form': article_group_form,
            'mbx': mbx
        }
        return render(request, 'xadmin/articlegroup/add.html', context)
    else:
        article_group_form = ArticleGroupForm.ArticleGroupForm(request.POST, request.FILES, instance=article_group_obj)
        if article_group_form.is_valid():
            article_group_form.save()
            action = '修改了文章组《%s》' % article_group_form.cleaned_data['name']
            add_log(request, action, siteid)
            return redirect('%s?siteid=%s' % (reverse('articlegrouplist'), siteid))
        else:
            error = article_group_form.errors
            #print(error)
            context = {
                'siteid': siteid,
                'article_group_form': article_group_form,
                'error': error,
                'mbx': mbx
            }
            return render(request, 'xadmin/articlegroup/add.html', context)

@login_required
@require_POST
def articlegroup_del(request):
    articlegroupid = request.POST.get('articlegroupid')
    mess = {}
    try:
        articlegroup_obj = ArticleGroup.objects.get(id=articlegroupid)
        articlegroup_obj.delete()
        mess['status'] = '200'
        mess['message'] = '删除成功'
        action = '删除了文章组《%s》' % articlegroup_obj.name
        add_log(request, action, articlegroup_obj.site_id)
    except Exception as e:
        mess['status'] = '500'
        mess['message'] = '删除失败'
    return JsonResponse(mess)


@login_required
@require_POST
def articlegroups_del(request):
    siteid = request.POST.get('siteid')
    ids = request.POST.get('ids')
    id_list = ids.split(',')
    mess = {}
    try:
        article_groups = ArticleGroup.objects.filter(id__in=id_list)
        article_groups.delete()
        mess['status'] = '200'
        mess['message'] = '删除成功'
        action = '删除了文章组%s' % ','.join(['《x》' % x.name for x in article_groups])
        add_log(request, action, siteid)
    except Exception as e:
        mess['status'] = '500'
        mess['message'] = '删除失败'
    return JsonResponse(mess)


@login_required
def selectArticleGroup(request):
    siteid = request.GET.get('siteid')
    groups = ArticleGroup.objects.filter(site_id=siteid)
    articleid = request.GET.get('articleid')
    article_obj = article=Article.objects.get(id=articleid)
    checked_group = ArticleGroup.objects.filter(article=article_obj)

    context = {
        'groups': groups,
        'siteid': siteid,
        'articleid': articleid,
        'checked_group': checked_group
    }
    return render(request, 'xadmin/articlegroup/selectArticleGroup.html', context)